package com.example.wordtris;


import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.content.ContentValues;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.AdapterView.OnItemSelectedListener;
public class settingActivity extends Activity {
	Button buttonFatto,buttonFatto1;
	String linguaDefault,nickName,codice,selected;
	private Locale locale = null;
	private WordtrisDatabaseHelper WordtrisDatabaseHelper;
	SQLiteDatabase db;
	Cursor cursor;
		@Override
		 public void onCreate(Bundle savedInstanceState) {
		        super.onCreate(savedInstanceState);
		        WordtrisDatabaseHelper = new WordtrisDatabaseHelper(this);
		        db = WordtrisDatabaseHelper.getReadableDatabase();
		        cursor =db.query("lingua", null, null, null, null, null, null);
		        cursor.moveToFirst();
        		//while (cursor.moveToFirst()) {
        			linguaDefault=cursor.getString(1);
        			//}
        			cursor.close();
        	        db.close();
        			Configuration config = getBaseContext().getResources().getConfiguration();
        	        locale = new Locale(linguaDefault);
        	        Locale.setDefault(locale);
        	        config.locale = locale;
        	        getBaseContext().getResources().updateConfiguration(config, 
        	        	    getBaseContext().getResources().getDisplayMetrics());	
		        setContentView(R.layout.setting_activity);
		        
		        db = WordtrisDatabaseHelper.getReadableDatabase();
		        cursor =db.query("nick", null, null, null, null, null, null);
		        cursor.moveToFirst();
		 		nickName=cursor.getString(1);
		 		cursor.close();
		        db.close();
		        final EditText nk=(EditText) findViewById(R.id.editText1);
		        String testo=nk.getText().toString();
		 		testo+=nickName;
		 		nk.setText(testo);
		        nk.setImeOptions(EditorInfo.IME_ACTION_DONE);
		        buttonFatto =(Button) findViewById(R.id.buttonFatto);
		        buttonFatto.setOnClickListener(new View.OnClickListener()//buttonFatto on
		         		{
		        	 @Override
		                public void onClick(View aView)
		                 {
		        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
		        	        mp1.start();
		        		String nome=nk.getText().toString();
		        		db = WordtrisDatabaseHelper.getWritableDatabase();
		             	ContentValues values = new ContentValues();
		             	values.put("nickName", nome);
		             	String whereClause = "idNick=?";
		             	String[] whereArgs = {"1" };
		             	db.update("nick", values,whereClause,whereArgs);
		             	db.close();
		                 }
		         		}	
		        		 );//buttonFatto off
		        List<String> lingue = new ArrayList<String>();
		        db = WordtrisDatabaseHelper.getReadableDatabase();
		        cursor =db.query("lingueSupportate", null, null, null, null, null, null);
		        if (cursor.moveToFirst()) {
		            do {
		                lingue.add(cursor.getString(1));
		            } while (cursor.moveToNext());
		        }
		        cursor.close();
		        db.close();

		        Spinner spinner = (Spinner)findViewById(R.id.langSpinner);
		        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
		        		this,
		        		android.R.layout.simple_spinner_item,
		        		lingue
		        		);
		        spinner.setAdapter(adapter);
		        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
		        	public void onItemSelected(AdapterView<?> adapter, View view,int pos, long id) {
		        		selected = (String)adapter.getItemAtPosition(pos);
		        		
		        	}
		        	public void onNothingSelected(AdapterView<?> arg0) {}
				});
		        buttonFatto1 =(Button) findViewById(R.id.buttonFatto1);
		        buttonFatto1.setOnClickListener(new View.OnClickListener()//buttonFatto on
		         		{
		        	 @Override
		                public void onClick(View aView)
		                 {
		        		 final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.beep9);  
		        	        mp1.start();
		        		SQLiteDatabase db = WordtrisDatabaseHelper.getReadableDatabase();
		        		String[] columns = { "codiceLingua" };
		        		String selection = "nomeLingua = ?";
		        		String[] selectionArgs = { selected };
		        		cursor=db.query("lingueSupportate", columns, selection, selectionArgs,null, null, null);
		        		while (cursor.moveToNext()) {
		        			codice = cursor.getString(0);
		        			}
		        		db = WordtrisDatabaseHelper.getWritableDatabase();
		             	ContentValues values = new ContentValues();
		             	values.put("lingua", codice);
		             	String whereClause = "idLingua=?";
		             	String[] whereArgs = {"1" };
		             	db.update("lingua", values,whereClause,whereArgs);
		             	cursor.close();
				        db.close();
		                 }
		        	 
		         		}	
		        		 );//buttonFatto off
		    }
}